﻿// owner 蒋家伟

#include "SerializeAndUnserializeUnitTest.h"

// std
#include <fstream>
#include <sstream>


// rapidjson
#include "rapidjson/writer.h"

// jsdk
#include "UnitTestDataDefine.h"
#include "UnitTestUtils.h"
#include "Dbg_Warn_Define.h"
#include "ObjectClassDefine.h"
#include "ObjectReflectDefine.h"
#include "CommonFieldDescriptor.h"
#include "JsonUtils.h"


using namespace std;
using namespace jsdk;

namespace
{
    bool save(const JsonDocument& jsonDoc, const std::wstring& path)
    {
        rapidjson::StringBuffer buffer;
        rapidjson::Writer<rapidjson::StringBuffer> write(buffer);
        jsonDoc.Accept(write);

        std::ofstream os(path);
        if (!os.is_open())
        {
            return false;
        }

        os << buffer.GetString();
        os.close();

        return true;
    }

    bool read(const std::wstring& path, JsonDocument& jsonDoc)
    {
        std::ifstream is(path);
        if (!is.is_open())
        {
            return false;
        }

        std::stringstream buffer;
        buffer << is.rdbuf();
        std::string jsonContent(buffer.str());

        return !jsonDoc.Parse(jsonContent.c_str(), jsonContent.size()).HasParseError();
    }

    bool serialize(const DBSchemaObject& dbobject, const std::wstring& path)
    {
        JsonObject jsonObject = JsonUtils::createObject();
        JsonDocument jsonDoc;
        jsonDoc.SetObject();

        bool success = dbobject.serialize(jsonDoc, jsonObject);
        jsonDoc.AddMember("100", std::move(jsonObject), jsonDoc.GetAllocator());

        return save(jsonDoc, path);
    }

    OwnerPtr<DBSchemaObject> unserialize(const std::wstring& path)
    {
        JsonDocument doc;
        bool success = ::read(path, doc);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(success && doc.IsObject(), L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");

        const JsonValue* pJsonObject = JsonUtils::getJsonValue(doc, "100");
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pJsonObject && pJsonObject->IsObject(), L"value不是Object对象", L"蒋家伟", L"2023-09-14");

        jsdk::UniqueId typeUid;
        success = CommonFieldDescriptor::getClassUidFromJsonObject(*pJsonObject, typeUid);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(success, L"缺少类别描述", L"蒋家伟", L"2023-12-07");

        OwnerPtr<DBSchemaObject> opDBSchemaObject = ObjectReflectUtils::getObjectReflectImp(typeUid, nullptr);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opDBSchemaObject, L"反射对象失败，类别为" + typeUid.toString(), L"蒋家伟", L"2023-11-29");

        const JsonValue* pValueObject = JsonUtils::getJsonValue(*pJsonObject, CommonFieldDescriptor::s_pValue_Key);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pValueObject && pValueObject->IsObject(), L"value不是Object对象", L"蒋家伟", L"2023-09-14");

        success = opDBSchemaObject->unserialize(*pValueObject);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(success, L"反流失败", L"蒋家伟", L"2023-11-29");
        return transformOwnerShip(opDBSchemaObject);
    }

    bool serialize(const std::vector<OwnerPtr<DBSchemaObject>>& dbobjects, const std::wstring& path)
    {
        JsonDocument jsonDoc;
        jsonDoc.SetObject();
        for (int i = 0; i < dbobjects.size(); i++)
        {
            UnitTestUtils::showPrecent(static_cast<uint16_t>(i * 100 / dbobjects.size()));
            DBG_WARN_AND_CONTINUE_UNLESS(dbobjects[i], L"Schema对象为空", L"蒋家伟", L"2023-12-07");

            JsonObject object = JsonUtils::createObject();
            DBG_WARN_AND_CONTINUE_UNLESS(dbobjects[i]->serialize(jsonDoc, object), dbobjects[i]->getClassName() + L"流化失败", L"蒋家伟", L"2023-12-07");
            JsonUtils::insert(jsonDoc, jsonDoc, StringUtils::toString(i).c_str(), std::move(object));
        }

        return ::save(jsonDoc, path);
    }

    //bool serialize(const std::vector<OwnerPtr<DBSchemaObject>>& dbobjects, const std::wstring& path)
    //{
    //    JsonDocument jsonDoc;
    //    jsonDoc.SetArray();
    //    for (int i = 0; i < dbobjects.size(); i++)
    //    {
    //        UnitTestUtils::showPrecent(static_cast<uint16_t>(i * 100 / dbobjects.size()));
    //        DBG_WARN_AND_CONTINUE_UNLESS(dbobjects[i], L"Schema对象为空", L"蒋家伟", L"2023-12-07");

    //        JsonObject object = JsonUtils::createObject();
    //        DBG_WARN_AND_CONTINUE_UNLESS(dbobjects[i]->serialize(jsonDoc, object), dbobjects[i]->getClassName() + L"流化失败", L"蒋家伟", L"2023-12-07");
    //        JsonUtils::insert(jsonDoc, object, "ID", jsdk::JsonValue(i));
    //        jsonDoc.PushBack(std::move(object), jsonDoc.GetAllocator());
    //    }

    //    return ::save(jsonDoc, path);
    //}

    bool unserialize(const std::wstring& path, std::vector<OwnerPtr<DBSchemaObject>>& res)
    {
        JsonDocument doc;
        bool success = ::read(path, doc);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success && doc.IsObject(), L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");

        int size = doc.MemberCount();
        int number = 0;

        for (auto iter = doc.MemberBegin(); iter != doc.MemberEnd(); ++iter)
        {
            UnitTestUtils::showPrecent(number++ * 100 / size);
            const JsonValue& jsonObjectValue = iter->value;
            DBG_WARN_AND_CONTINUE_UNLESS(jsonObjectValue.IsObject(), L"文档存储应该全是obejct", L"蒋家伟", L"2023-12-07");
            jsdk::UniqueId typeUid;
            success = CommonFieldDescriptor::getClassUidFromJsonObject(jsonObjectValue, typeUid);
            DBG_WARN_AND_CONTINUE_UNLESS(success, L"缺少类别描述", L"蒋家伟", L"2023-12-07");
            OwnerPtr<DBSchemaObject> opDBSchemaObject = ObjectReflectUtils::getObjectReflectImp(typeUid, nullptr);
            DBG_WARN_AND_CONTINUE_UNLESS(opDBSchemaObject, L"反射对象失败，类别为" + typeUid.toString(), L"蒋家伟", L"2023-11-29");
            const JsonValue* pJsonSourceValue = JsonUtils::getJsonValue(jsonObjectValue, CommonFieldDescriptor::s_pValue_Key);
            DBG_WARN_AND_CONTINUE_UNLESS(pJsonSourceValue, L"获取值失败，类别为" + typeUid.toString(), L"蒋家伟", L"2023-11-29");
            success = opDBSchemaObject->unserialize(*pJsonSourceValue);
            DBG_WARN_AND_CONTINUE_UNLESS(success, L"反流失败", L"蒋家伟", L"2023-11-29");
            res.push_back(transformOwnerShip(opDBSchemaObject));
        }

        return true;
    }

    //bool unserialize(const std::wstring& path, std::vector<OwnerPtr<DBSchemaObject>>& res)
    //{
    //    JsonDocument doc;
    //    bool success = ::read(path, doc);
    //    DBG_WARN_AND_RETURN_FALSE_UNLESS(success && doc.IsArray(), L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");

    //    int size = doc.Size();
    //    int number = 0;

    //    for (auto iter = doc.Begin(); iter != doc.End(); ++iter)
    //    {
    //        UnitTestUtils::showPrecent(number++ * 100 / size);
    //        const JsonValue& jsonObjectValue = *iter;
    //        DBG_WARN_AND_CONTINUE_UNLESS(jsonObjectValue.IsObject(), L"文档存储应该全是obejct", L"蒋家伟", L"2023-12-07");
    //        std::wstring typeStr;
    //        success = CommonFieldDescriptor::getClassTypeKeyStr(jsonObjectValue, typeStr);
    //        DBG_WARN_AND_CONTINUE_UNLESS(success, L"缺少类别描述", L"蒋家伟", L"2023-12-07");
    //        OwnerPtr<DBSchemaObject> opDBSchemaObject = ObjectReflectUtils::getObjectReflectImp(jsdk::UniqueId::fromWString(typeStr), nullptr);
    //        DBG_WARN_AND_CONTINUE_UNLESS(opDBSchemaObject, L"反射对象失败，类别为" + typeStr, L"蒋家伟", L"2023-11-29");
    //        const JsonValue* pJsonSourceValue = JsonUtils::getJsonValue(jsonObjectValue, CommonFieldDescriptor::s_pValue_Key);
    //        DBG_WARN_AND_CONTINUE_UNLESS(pJsonSourceValue, L"获取值失败，类别为" + typeStr, L"蒋家伟", L"2023-11-29");
    //        success = opDBSchemaObject->unserialize(*pJsonSourceValue);
    //        DBG_WARN_AND_CONTINUE_UNLESS(success, L"反流失败", L"蒋家伟", L"2023-11-29");
    //        res.push_back(transformOwnerShip(opDBSchemaObject));
    //    }

    //    return true;
    //}

    void deleteFile(const std::wstring& path)
    {
        ::remove(StringUtils::toString(path).c_str());
    }
}

UNIT_TEST_START(jsdk, SerializeAndUnserializeUnitTest)
{
    SerializeAndUnserializeUnitTest test;

    bool success = test.testSerializeAndUnserialize();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试对象序列化与反序列化失败", L"蒋家伟", L"2023-11-13");

    success = test.testVectorSerializeAndUnserialize();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试vector对象序列化与反序列化失败", L"蒋家伟", L"2023-11-13");

    success = test.testVectorValueOwnerptrSerializeAndUnserialize();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试vector对象值为Ownptr序列化与反序列化失败", L"蒋家伟", L"2023-11-13");

    success = test.testSetSerializeAndUnserialize();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试set对象序列化与反序列化失败", L"蒋家伟", L"2023-11-13");

    success = test.testMapSerializeAndUnserialize();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试Map对象序列化与反序列化失败", L"蒋家伟", L"2023-11-13");

    //success = test.testVectorOwnerPtrMemoryAndSpeed();
    //DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"测试vector对象值为Ownptr序列化与反序列化速度与内存占用失败", L"蒋家伟", L"2023-11-13");

    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testSerializeAndUnserialize() const
{
    OwnerPtr<TestB> opSerializeTestB = ObjectReflectUtils::getObjectReflect<TestB>();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opSerializeTestB, L"TestB对象反射失败", L"蒋家伟", L"2023-09-14");
    opSerializeTestB->setopITestA2Test(NEW_AS_OWNERPTR(TestA2, std::wstring( L"测试子\0对象A2", 8), 32, 16));
    opSerializeTestB->setopTestATest(NEW_AS_OWNERPTR(TestA, L"测试对象A", 20));
    opSerializeTestB->setFloatTest(100.51f);

    const std::wstring filePath = L"./TestB.json";

    {
        // 序列化
        bool success = ::serialize(*opSerializeTestB, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    {
        // 反序列化

        OwnerPtr<DBSchemaObject> opDBSchemaObject = ::unserialize(filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opDBSchemaObject, L"反射对象失败", L"蒋家伟", L"2023-11-29");
        OwnerPtr<TestB> opUnserialize = transformOwnerShipAndCast<TestB>(transformOwnerShip(opDBSchemaObject));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(*opUnserialize == *opSerializeTestB, L"返回对象与原始对象不同", L"蒋家伟", L"2023-11-29");
    }

    deleteFile(filePath);

    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testVectorSerializeAndUnserialize() const
{
    TestVectorBaseType a;
    std::vector<std::string>& strs = a.getFWDbVectorStr();
    for (int i = 0; i < 100; i++)
    {
        strs.push_back("Test:" + StringUtils::toString(i));
    }


    const std::wstring filePath = L"./TestVector.json";
    {
        // 序列化
        bool success = ::serialize(a, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    {
        // 反序列化
        OwnerPtr<DBSchemaObject> opDBSchemaObject = ::unserialize(filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opDBSchemaObject, L"反射对象失败", L"蒋家伟", L"2023-11-29");
        OwnerPtr<TestVectorBaseType> opUnserialize = transformOwnerShipAndCast<TestVectorBaseType>(transformOwnerShip(opDBSchemaObject));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(*opUnserialize == a, L"返回对象与原始对象不同", L"蒋家伟", L"2023-11-29");
    }

    deleteFile(filePath);


    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testVectorValueOwnerptrSerializeAndUnserialize() const
{
    TestVectorOwnerPtr a;
    std::vector<OwnerPtr<TestA>>& testAs = a.getFWDbVectorTestA();
    testAs.push_back(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr A_1", 1));
    testAs.push_back(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr A_2", 2));
    testAs.push_back(nullptr);
    testAs.push_back(nullptr);
    testAs.push_back(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr A_5", 5));

    std::vector<OwnerPtr<ITestA>>& testIAs = a.getFWDbVectorITestA();
    testIAs.push_back(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr A_1", 1));
    testIAs.push_back(nullptr);
    testIAs.push_back(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr A2_1", 1, 2));
    testIAs.push_back(nullptr);
    testIAs.push_back(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr A_2", 5));
    testIAs.push_back(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr A2_2", 3, 4));

    std::vector<OwnerPtr<TestB>>& testB = a.getFWDbVectorTestB();
    testB.push_back(nullptr);
    OwnerPtr<TestB> opTestB1 = NEW_AS_OWNERPTR(TestB, L"TestVectorOwnerPtr B_1", 1, ElementId(10));
    opTestB1->setopITestATest(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr B_IA_A2_1", 2, 1));
    opTestB1->setopITestA2Test(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_IA_A_1", 2));
    opTestB1->setopTestATest(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_A_A1_2", 3));
    opTestB1->setFloatTest(-10.56f);

    testB.push_back(transformOwnerShip(opTestB1));

    OwnerPtr<TestB> opTestB2 = NEW_AS_OWNERPTR(TestB, L"TestVectorOwnerPtr B_2", 2, ElementId(20));
    opTestB2->setopITestATest(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr B_IA_A2_2", 2, 1));
    opTestB2->setopITestA2Test(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr B_IA_A2_3", 2, 2));
    opTestB2->setopTestATest(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_A_A1_3", 3));
    opTestB2->setFloatTest(0.0f);

    testB.push_back(transformOwnerShip(opTestB2));

    OwnerPtr<TestB> opTestB3 = NEW_AS_OWNERPTR(TestB, L"TestVectorOwnerPtr B_3", 3, ElementId(30));
    opTestB3->setopITestATest(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_IA_A1_4", 2));
    opTestB3->setopITestA2Test(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_IA_A1_5", 2));
    opTestB3->setopTestATest(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_A_A1_6", 3));
    opTestB3->setFloatTest(5.1f);
    testB.push_back(transformOwnerShip(opTestB3));

    testB.push_back(nullptr);
    OwnerPtr<TestB> opTestB4  = NEW_AS_OWNERPTR(TestB, L"TestVectorOwnerPtr B_4", 4, ElementId(40));
    testB.push_back(transformOwnerShip(opTestB4));


    const std::wstring filePath = L"./TestVectorOwnerPtr.json";

    {
        // 序列化
        bool success = ::serialize(a, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    {
        // 反序列化
        OwnerPtr<DBSchemaObject> opDBSchemaObject = ::unserialize(filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opDBSchemaObject, L"反射对象失败", L"蒋家伟", L"2023-11-29");
        OwnerPtr<TestVectorOwnerPtr> opUnserialize = transformOwnerShipAndCast<TestVectorOwnerPtr>(transformOwnerShip(opDBSchemaObject));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(*opUnserialize == a, L"返回对象与原始对象不同", L"蒋家伟", L"2023-11-29");
    }

    deleteFile(filePath);

    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testSetSerializeAndUnserialize()
{
    TestSetBaseType test;
    constexpr int size = 10000;
    std::set<OwnerPtr<TestA>, OwnerPtrTestACP>& setTestA = test.getDbSetTestAFw();
    std::set<std::wstring>& setTestStr = test.getDbSetStrFw();
    for (int i = 0; i < size; i++)
    {
        setTestA.insert(NEW_AS_OWNERPTR(TestA, L"TestSetBaseType_TestA" + StringUtils::toWString(i), i));
        setTestStr.insert(L"测试数据集合" + StringUtils::toWString(i));
    }

    const std::wstring filePath = L"./TestSetBaseType.json";
    DBG_WARN(L"Set序列化开始", L"蒋家伟", L"2023-12-07");

    {
        // 序列化
        bool success = ::serialize(test, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    DBG_WARN(L"Set反序列化开始", L"蒋家伟", L"2023-12-07");
    {
        // 反序列化
        OwnerPtr<DBSchemaObject> opDBSchemaObject = ::unserialize(filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opDBSchemaObject, L"TestSetBaseType反序列化失败", L"蒋家伟", L"2023-11-29");
        OwnerPtr<TestSetBaseType> opTestSetBaseType = transformOwnerShipAndCast<TestSetBaseType>(opDBSchemaObject);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opTestSetBaseType && *opTestSetBaseType == test, L"cast_TestSetBaseTyp化失败", L"蒋家伟", L"2023-11-29");
    }

    deleteFile(filePath);

    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testMapSerializeAndUnserialize()
{
    TestMap test;
    constexpr int size = 10000;
    std::map<std::wstring, OwnerPtr<TestA>>& stringToTestAMap = test.getStringToTestAMapFw();
    std::map<int32_t, std::string>& intToStringMap = test.getIntToStringMapFw();

    std::unordered_map<std::wstring, OwnerPtr<TestA>>& stringToTestAUMap = test.getStringToTestAUMapFw();
    std::unordered_map<int32_t, std::string>& intToStringUMap = test.getIntToStringUMapFw();

    for (int i = 0; i < size; i++)
    {
        stringToTestAMap.insert(
            std::make_pair(
                StringUtils::toWString(i), 
                NEW_AS_OWNERPTR(TestA, L"TestMap_TestA" + StringUtils::toWString(i), i)
            )
        );
        intToStringMap.insert(
            std::make_pair(
                i,
                "Test DataMap" + StringUtils::toString(i)
            )
           );

        stringToTestAUMap.insert(
            std::make_pair(
                StringUtils::toWString(i),
                NEW_AS_OWNERPTR(TestA, L"TestUMap_TestA" + StringUtils::toWString(i), i)
            )
        );
        intToStringUMap.insert(
            std::make_pair(
                i,
                "TestUMap_TestA" + StringUtils::toString(i)
            )
        );
    }

    const std::wstring filePath = L"./TestMap.json";
    DBG_WARN(L"Map序列化开始", L"蒋家伟", L"2023-12-07");

    {
        // 序列化
        bool success = ::serialize(test, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    DBG_WARN(L"Map反序列化开始", L"蒋家伟", L"2023-12-07");
    {
        // 反序列化
        OwnerPtr<DBSchemaObject> opDBSchemaObject = ::unserialize(filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opDBSchemaObject, L"TestSetBaseType反序列化失败", L"蒋家伟", L"2023-11-29");
        OwnerPtr<TestMap> opTestMap = transformOwnerShipAndCast<TestMap>(opDBSchemaObject);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opTestMap && *opTestMap == test, L"Map对比失败", L"蒋家伟", L"2023-11-29");
    }

    deleteFile(filePath);

    return true;
}

bool jsdk::SerializeAndUnserializeUnitTest::testVectorOwnerPtrMemoryAndSpeed() const
{
    std::vector<OwnerPtr<DBSchemaObject>> dbSchemaObject;
    constexpr int size = 200000;
    for (int i = 0; i < size; i++)
    {
       OwnerPtr<TestVectorOwnerPtr> opTestVectorOwnerPtr =  NEW_AS_OWNERPTR(TestVectorOwnerPtr);
       DBG_WARN_AND_CONTINUE_UNLESS(opTestVectorOwnerPtr, L"TestVectorOwnerPtr对象创建失败", L"蒋家伟", L"2023-12-07");
       std::vector<OwnerPtr<TestB>>& testB = opTestVectorOwnerPtr->getFWDbVectorTestB();
       for (int j = 0; j < 2; j++)
       {
           OwnerPtr<TestB> opTestB1 = NEW_AS_OWNERPTR(TestB, L"TestVectorOwnerPtr B_1", 1, ElementId(10));
           opTestB1->setopITestATest(NEW_AS_OWNERPTR(TestA2, L"TestVectorOwnerPtr B_IA_A2_1", 2, 1));
           opTestB1->setopITestA2Test(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_IA_A_1", 2));
           opTestB1->setopTestATest(NEW_AS_OWNERPTR(TestA, L"TestVectorOwnerPtr B_A_A1_2", 3));
           opTestB1->setFloatTest(-10.56f);
           testB.push_back(transformOwnerShip(opTestB1));
       }
       dbSchemaObject.push_back(transformOwnerShip(opTestVectorOwnerPtr));
    }

    const std::wstring filePath = L"./TestVectorOwnerPtrMemoryAndSpeed.json";
    DBG_WARN(L"序列化开始", L"蒋家伟", L"2023-12-07");

    {
        // 序列化
        bool success = ::serialize(dbSchemaObject, filePath);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"序列化的json文件打开失败", L"蒋家伟", L"2023-09-14");
    }

    DBG_WARN(L"反序列化开始", L"蒋家伟", L"2023-12-07");
    {
        // 反序列化
        std::vector<OwnerPtr<DBSchemaObject>> res;
        bool success = ::unserialize(filePath, res);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"反射对象失败", L"蒋家伟", L"2023-11-29");
        DBG_WARN_AND_RETURN_FALSE_UNLESS(res.size() == dbSchemaObject.size(), L"反序列化对象数量错误", L"蒋家伟", L"2023-12-07");
        for (int i = 0; i < size; i++)
        {
            OwnerPtr<TestVectorOwnerPtr> opUnserializeSource = transformOwnerShipAndCast<TestVectorOwnerPtr>(transformOwnerShip(dbSchemaObject[i]));
            OwnerPtr<TestVectorOwnerPtr> opUnserializeTarget = transformOwnerShipAndCast<TestVectorOwnerPtr>(transformOwnerShip(res[i]));
            DBG_WARN_AND_RETURN_FALSE_UNLESS(*opUnserializeSource == *opUnserializeTarget, L"返回对象与原始对象不同", L"蒋家伟", L"2023-11-29");
        }
    }

    deleteFile(filePath);
    return true;
}

